package com.bigdata.service.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.bigdata.common.annotation.RepeatSubmit;
import com.bigdata.common.annotation.Log;
import com.bigdata.common.core.controller.BaseController;
import com.bigdata.common.core.domain.PageQuery;
import com.bigdata.common.core.domain.R;
import com.bigdata.common.core.validate.AddGroup;
import com.bigdata.common.core.validate.EditGroup;
import com.bigdata.common.core.validate.QueryGroup;
import com.bigdata.common.enums.BusinessType;
import com.bigdata.common.utils.poi.ExcelUtil;
import com.bigdata.service.domain.vo.UserPointsVo;
import com.bigdata.service.domain.bo.UserPointsBo;
import com.bigdata.service.service.IUserPointsService;
import com.bigdata.common.core.page.TableDataInfo;

/**
 * 用户积分账户
 *
 * @author ruoyi
 * @date 2025-07-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/service/points")
public class UserPointsController extends BaseController {

    private final IUserPointsService iUserPointsService;

    /**
     * 查询用户积分账户列表
     */
    @SaCheckPermission("service:points:list")
    @GetMapping("/list")
    public TableDataInfo<UserPointsVo> list(UserPointsBo bo, PageQuery pageQuery) {
        return iUserPointsService.selectUserPageList(bo, pageQuery);
    }

    /**
     * 导出用户积分账户列表
     */
    @SaCheckPermission("service:points:export")
    @Log(title = "用户积分账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserPointsBo bo, HttpServletResponse response) {
        List<UserPointsVo> list = iUserPointsService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户积分账户", UserPointsVo.class, response);
    }

    /**
     * 获取用户积分账户详细信息
     *
     * @param userId 主键
     */
    @SaCheckPermission("service:points:query")
    @GetMapping("/{userId}")
    public R<UserPointsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userId) {
        return R.ok(iUserPointsService.queryById(userId));
    }

    /**
     * 新增用户积分账户
     */
    @SaCheckPermission("service:points:add")
    @Log(title = "用户积分账户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserPointsBo bo) {
        return toAjax(iUserPointsService.insertByBo(bo));
    }

    /**
     * 修改用户积分账户
     */
    @SaCheckPermission("service:points:edit")
    @Log(title = "用户积分账户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserPointsBo bo) {
        return toAjax(iUserPointsService.updateByBo(bo));
    }

    /**
     * 删除用户积分账户
     *
     * @param userIds 主键串
     */
    @SaCheckPermission("service:points:remove")
    @Log(title = "用户积分账户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] userIds) {
        return toAjax(iUserPointsService.deleteWithValidByIds(Arrays.asList(userIds), true));
    }
}
